#!/usr/bin/perl
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

package CCNCore::spool::cdn_downstream_high_completed;
use strict; use warnings;

=pod

    This is ran on the GUI node that requested the distribution
    once all the rsync operations to the CDN nodes have been completed.

=cut
use CCNCore::Provisioning::Local;
use CCNCore::Provision::Distributed;
use CCNCore::IPC::Ticket;

sub run
{
    my $id = pop;
    my $what = shift;

    # Retrieve the callback information from the original ticket
    # (e.g. the one created by create_sqlite).
    my $t2 = new CCNCore::IPC::Ticket($id);
    my $callback = $t2->retrieve_context('callback');
    my $ticket = $t2->retrieve_context('ticket');

    my $target_pk = pop; # Optional: target server pk

    my $p = new CCNCore::Provisioning::Local;
    my $c = new CCNCore::Provision::Distributed($p);

    # Notify all the CDN downstream servers to distribute.
    # Note: the command is specific to the content, since not all
    # server will need all content.
    my $t1 = new CCNCore::IPC::Ticket;
    $t1->add_context('callback',$callback);
    $t1->add_context('ticket',$ticket);
    my $count = $c->send_cdn_high($t1,"cdn-downstream-low-$what".(defined($target_pk)?" $target_pk":''));
    $t1->add_action($count,'wait');

    # Clear our local (GUI node) ticket, indicating all the "high" piece is completed.
    $t2->completed();
    $t2->clear_ticket();
}

1;